<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">

            <h:form id="cepForm" enctype="multipart/form-data">
                <h1 class="title">Titulo e Documentos</h1>
                <p:growl id="messages" showDetail="true" autoUpdate="true"  /> 
                <div class="row">
                    <div class="twelve columns"> 
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Cadastro</h4></legend>
                            <p:panel id="charterPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix ">


                                <p:outputLabel value="Número: " styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocumentEdit.register}"/>

                                <p:outputLabel value="Tipo: " styleClass="grid_4"/>
                                <div class="grid_8">
                                    <p:selectOneMenu style="width: 103%;" panelStyle="width:150px;" value="#{managerSearchTitleDocument.titleDocumentEdit.typeTitleDocument}">  
                                        <f:selectItem itemLabel="" itemValue="" />  
                                        <f:selectItems value="#{managerSearchTitleDocument.typeTitleDocumentList()}" />  
                                    </p:selectOneMenu> 
                                </div>
                                <div class="clear"/>

                                <p:outputLabel value="Protocolo: " styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocumentEdit.protocolo}"/>   

                                <p:outputLabel value="Livro Protocolo: " styleClass="grid_4"/>                      
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocumentEdit.bookprot}"/>   

                                <div class="clear"/>

                                <p:outputLabel value="Data Protocolo: " styleClass="grid_3"/>
                                <p:calendar showOn="button" styleClass="grid_8" style="width:32.55%"
                                            pattern="dd/MM/yyyy"
                                            value="#{managerSearchTitleDocument.titleDocumentEdit.datebp}"/>

                                <p:outputLabel value="Data Titulo: " styleClass="grid_4"/>
                                <p:calendar showOn="button" styleClass="grid_8" style="width:33%"
                                            pattern="dd/MM/yyyy"
                                            value="#{managerSearchTitleDocument.titleDocumentEdit.datetdoc}"/>


                                <div class="clear"/>

                                <p:outputLabel value="Livro: " styleClass="grid_3" />
                                <p:inputMask mask="?999999999" placeHolder="" styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocumentEdit.book}"/>

                                <p:outputLabel value="Livro Compl: " styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocumentEdit.bookComp}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="Folha: " styleClass="grid_3" />
                                <p:inputMask mask="?999999999" placeHolder="" styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocumentEdit.leaf}"/> 

                                <p:outputLabel value="Folha Compl: " styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" value="#{managerSearchTitleDocument.titleDocumentEdit.leafComp}"/> 


                                <div class="clear"/>
                                <p:outputLabel value="Objeto: " styleClass="grid_3"/>
                                <p:inputTextarea cols="101" rows="4" maxlength="1000"
                                                 styleClass="grid_20" value="#{managerSearchTitleDocument.titleDocumentEdit.obj}"/> 

                                <div class="clear"/>
                                <p:outputLabel value="Observações: " styleClass="grid_3"/>
                                <p:inputTextarea cols="100" rows="4" maxlength="1000"
                                                 styleClass="grid_20" value="#{managerSearchTitleDocument.titleDocumentEdit.obs}"/> 
                            </p:panel>

                        </fieldset>
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Partes</h4></legend>

                            <p:panel id="partsPanel" style="background: none; border: none;" styleClass="container_24 clearfix ">

                                <p:outputLabel value="Pessoa: "  styleClass="grid_3"/>
                                <p:selectOneRadio id="typePerson" style="margin-top:-8px;" styleClass="grid_8"
                                                  value="#{managerSearchTitleDocument.peopleEdit.tipoDocumento}" >  
                                    <f:selectItems value="#{managerSearchTitleDocument.typeDocumentList()}" />
                                    <p:ajax event="change" update=":cepForm:partsPanel" 
                                            listener="#{managerSearchTitleDocument.typeDocumentPeople}" />
                                </p:selectOneRadio>

                                <div class="clear"/>

                                <p:outputLabel id="typeDocumentLabel" 
                                               value="#{managerSearchTitleDocument.peopleEdit.tipoDocumento}: " 
                                               styleClass="grid_3"/>

                                <p:inputMask id="typeDocumentInput" 
                                             value="#{managerSearchTitleDocument.peopleEdit.tipoDocumento.type eq 'CPF' ? 
                                                      managerSearchTitleDocument.peopleEdit.cpf : managerSearchTitleDocument.peopleEdit.cnpj}"
                                             mask="#{managerSearchTitleDocument.peopleEdit.tipoDocumento.type eq 'CPF' ? 
                                                     '999.999.999-99':'99.999.999/9999-99'}"
                                             styleClass="grid_8" > 
                                    <p:ajax event="blur" update=":cepForm:partsPanel" onstart="bui.show()" oncomplete="bui.hide()"
                                            listener="#{managerSearchTitleDocument.ajaxVerifyPeopleExist()}" />
                                </p:inputMask>

                                <p:blockUI block=":cepForm:partsPanel" widgetVar="bui"> 
                                    <p:graphicImage value="media/css/images/ajax-loader.gif"/>
                                </p:blockUI>

                                <div class="clear"/>

                                <p:outputLabel value="Nome: " styleClass="grid_3" />
                                <p:inputText styleClass="grid_20" value="#{managerSearchTitleDocument.peopleEdit.name}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="RG: " rendered="#{managerSearchTitleDocument.peopleEdit.tipoDocumento.type eq 'CPF'}" styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" rendered="#{managerSearchTitleDocument.peopleEdit.tipoDocumento.type eq 'CPF'}" value="#{managerSearchTitleDocument.peopleEdit.rg}"/> 

                                <p:outputLabel value="Orgão emissor: " rendered="#{managerSearchTitleDocument.peopleEdit.tipoDocumento.type eq 'CPF'}" styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" rendered="#{managerSearchTitleDocument.peopleEdit.tipoDocumento.type eq 'CPF'}" value="#{managerSearchTitleDocument.peopleEdit.organ}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="Data nasc: " rendered="#{managerSearchTitleDocument.peopleEdit.tipoDocumento.type eq 'CPF'}" styleClass="grid_3"/>
                                <p:calendar showOn="button" styleClass="grid_8" rendered="#{managerSearchTitleDocument.peopleEdit.tipoDocumento.type eq 'CPF'}" style="width:32.44%" 
                                            value="#{managerSearchTitleDocument.peopleEdit.nasc}"/> 

                                <p:outputLabel value="Qualidade: " 
                                               styleClass="#{managerSearchTitleDocument.peopleEdit.tipoDocumento.type eq 'CPF' ? 'grid_4':'grid_3'}"/>
                                <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true" converter="qualityconverter"   
                                                var="qualityValue" itemLabel="#{qualityValue.description}" scrollHeight="200"
                                                itemValue="#{qualityValue}" value="#{managerSearchTitleDocument.partEdit.partQuality}" 
                                                completeMethod="#{managerSearchTitleDocument.autocompleteQuality}" />

                                <div class="clear"/>

                                <div class="buttonAction">
                                    <p:commandButton value="Adicionar" actionListener="#{managerSearchTitleDocument.addPartsEdit()}"
                                                     icon="ui-icon-plus"
                                                     process="@this, :cepForm:partsPanel, 
                                                     :cepForm:dataTableParts"
                                                     update=":cepForm:partsPanel, 
                                                     :cepForm:dataTableParts" styleClass="ui-priority-primary" />
                                    <p:commandButton value="Limpar"  icon="ui-icon-trash" />
                                </div>


                                <p:dataTable value="#{managerSearchTitleDocument.titleDocumentEdit.parts}"
                                             emptyMessage="Nenhum registro encontrado"
                                             rowIndexVar="rowid"
                                             id="dataTableParts" var="partTable">
                                    <f:facet name="header">
                                        Partes
                                    </f:facet>
                                    <p:column headerText="Nome" width="550">
                                        #{partTable.people.name}
                                    </p:column>

                                    <p:column headerText="Documento">
                                        #{partTable.people.cpf}
                                        #{partTable.people.cnpj}
                                    </p:column>

                                    <p:column headerText="Qualidade">
                                        #{partTable.partQuality.description}
                                    </p:column>
                                    <p:column headerText="Ação">
                                        <p:commandButton  style="width: 20px; height: 20px;" 
                                                          process="@this, :cepForm:dataTableParts"
                                                          update=":cepForm:dataTableParts"
                                                          actionListener="#{managerSearchTitleDocument.removePart(rowid)}"
                                                          icon="ui-icon-trash"/>
                                    </p:column>

                                </p:dataTable>

                            </p:panel>
                        </fieldset>
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Selos</h4></legend>

                            <p:panel id="stampPanel" 
                                     style="background: none; border: none;" styleClass="container_24 clearfix ">

                                <p:outputLabel value="Série: "   styleClass="grid_3"/>
                                <p:inputText  styleClass="grid_8" value="#{managerSearchTitleDocument.stampEdit.serie}"/> 

                                <p:outputLabel value="Número: "  styleClass="grid_4"/>
                                <p:inputText  styleClass="grid_8" value="#{managerSearchTitleDocument.stampEdit.number}"/> 

                                <p:outputLabel value="Tipo Selo: "  styleClass="grid_3" />
                                <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true"
                                                converter="typestampconverter"   forceSelection="true"
                                                value="#{managerSearchTitleDocument.typeStampObj}"
                                                var="typeStampValue" itemLabel="#{typeStampValue.name}" 
                                                itemValue="#{typeStampValue}"
                                                completeMethod="#{managerTypeStamp.autoComplete}" >

                                    <p:column style="width:20%;text-align:center;background-color: \##{typeStampValue.color};">  
                                    </p:column>  

                                    <p:column style="width:80%">  
                                        #{typeStampValue.name} 
                                    </p:column>
                                </p:autoComplete>

                                <div class="clear"/>

                                <div class="buttonAction">
                                    <p:commandButton value="Adicionar" actionListener="#{managerSearchTitleDocument.addStampsEdit()}"
                                                     icon="ui-icon-plus" 
                                                     process="@this, :cepForm:stampPanel, 
                                                     :cepForm:dataTableStamps"
                                                     update=":cepForm:stampPanel, 
                                                     :cepForm:dataTableStamps" styleClass="ui-priority-primary" />
                                    <p:commandButton value="Limpar"  icon="ui-icon-trash" />
                                </div>

                                <p:dataTable id="dataTableStamps"
                                             value="#{managerSearchTitleDocument.titleDocumentEdit.stamp}"
                                             var="stampTable"
                                             rowIndexVar="rowidStamp"
                                             emptyMessage="Nenhum registro encontrado" >
                                    <f:facet name="header">
                                        Selos
                                    </f:facet>
                                    <p:column headerText="Série">
                                        #{stampTable.serie}
                                    </p:column>
                                    <p:column headerText="Número">
                                        #{stampTable.number}
                                    </p:column>
                                    <p:column headerText="Tipo">
                                        #{stampTable.typeStamp.name}
                                    </p:column>
                                    <p:column headerText="Ação">
                                        <p:commandButton  style="width: 20px; height: 20px;" 
                                                          process="@this, :cepForm:dataTableStamps"
                                                          update=":cepForm:dataTableStamps"
                                                          actionListener="#{managerSearchTitleDocument.removeStamp(rowidStamp)}"
                                                          icon="ui-icon-trash"/>
                                    </p:column>

                                </p:dataTable>
                            </p:panel>
                        </fieldset>
                        <br/>

                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Anexos</h4></legend>

                            <p:fileUpload 
                                mode="advanced"  
                                label="Anexar"
                                fileUploadListener="#{managerSearchTitleDocument.handle}"
                                multiple="true" 
                                auto="true"
                                invalidFileMessage="Tipo de arquivo não suportado."
                                invalidSizeMessage="Tamanho do arquivo não é suportado"
                                uploadLabel="Enviar"
                                cancelLabel="Cancelar"
                                sizeLimit="1485760"
                                immediate="true"
                                update="annexPanel,messages"
                                allowTypes="/(\.|\/)(gif|tif|tiff|jpe?g|png|pdf)$/"/> 


                            <p:panel id="annexPanel" 
                                     style="background: none; border: none;" styleClass="container_24 clearfix ">

                                <ui:repeat  value="#{managerSearchTitleDocument.titleDocumentEdit.annex}" var="a"  >
                                    <h:outputLink  

                                        value="#{a.type ne 'application/pdf' ? 'viewImage':'show'}"
                                        title="#{a.description}"  target="_blank" >

                                        <div style="text-align: center; width: 20%; height:220px;" class="grid_4">

                                            <p:graphicImage styleClass="image grid_3" rendered="#{a.type ne 'application/pdf'}"
                                                            style="margin-left:1%; width: 100px;"
                                                            url="show/?annex=#{a.id}" />

                                            <p:outputLabel  value="#{a.description.length() ge 10 ? a.description.substring(0, 10).concat('...') : 
                                                                     a.description.replace('.png','').replace(' ','_')}" 
                                                            style="margin-left:1%; width: 100px;" rendered="#{a.type ne 'application/pdf'}"/>

                                            <p:graphicImage styleClass="image grid_3" rendered="#{a.type eq 'application/pdf'}"
                                                            style="margin-left:1%; width: 100px;" 
                                                            url="assets/images/pdf.jpeg" />

                                            <div class="grid_20">
                                                <p:outputLabel  value="#{a.description.length() ge 10 ? a.description.substring(0, 10).concat('...') : 
                                                                         a.description.replace('.pdf','').replace(' ','_')}" 
                                                                styleClass="grid_18" rendered="#{a.type eq 'application/pdf'}"/>
                                            </div>
                                            <p:commandButton icon="ui-icon-close" style="height:30px;"
                                                             label="Remover Anexo"
                                                             oncomplete="dlgExcluirAnexo.show();"
                                                             actionListener="#{managerSearchTitleDocument.removeAnnexAux(a)}"
                                                             value="Remover" styleClass="grid_15"/>
                                        </div>
                                        <f:param name="annex" value="#{a.id}"/>
                                    </h:outputLink>
                                </ui:repeat>
                            </p:panel>
                        </fieldset>

                        <div class="buttonAction">
                            <p:commandButton value="Atualizar" icon="ui-icon-disk"
                                             process="@form" update="@form"
                                             actionListener="#{managerSearchTitleDocument.update()}" styleClass="ui-priority-primary" />
                            <p:commandButton value="Cancelar"  icon="ui-icon-trash" />
                        </div >
                    </div>
                </div>

                <p:dialog header="Exclusão de Anexo" widgetVar="dlgExcluirAnexo" modal="true"
                          showEffect="fade" hideEffect="fade" resizable="false" height="70">  
                    <h:outputText value="Deseja Excluir o Anexo ?" /> <br/> 
                    <f:facet name="footer">
                        <center>
                            <p:commandButton value="Sim" icon="ui-icon-check"
                                             update=":cepForm:annexPanel" oncomplete="dlgExcluirAnexo.hide();"
                                             actionListener="#{managerSearchTitleDocument.removeAnnex()}"  />

                            <p:commandButton value="Não" icon="ui-icon-close"
                                             onclick="dlgExcluirAnexo.hide();"  />
                        </center>
                    </f:facet>

                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
